import axios from 'axios';
import configs from './configs';
import actions from '../index';
import { async } from 'rsvp';
import Storage from '../../../util/Storage'
import {message} from 'antd';
import Qs from 'qs';
let  jwtDecode = require('jwt-decode');
/* import { applyPatches } from '_immer@1.10.0@immer'; */
const {

    GET_USER_MENU_SUCCESS,
    GET_USER_MENU_FAILURE,
    CHANGE_PAGE_SUCCSEE,

    GET_USERS_LOGIN_SUCCESS,
    GET_USERS_LOGIN_FAILURE,

    GET_REGISTER_SUCCESS,
    GET_REGISTER_FAILURE,
    GET_VERIFICATION_SUCCESS,
    GET_ESENDEMAIL_SUCCESS
}=actions;

const baseUrl=configs.baseUrl;

export function sendRes(apply)
{
    return async(dispatch)=>{
        console.log(apply);
        axios({
            method: 'POST',
            url: `${baseUrl}user.do/register`,
            data: JSON.stringify(apply),
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        dataType:'json',
          
        }).then((res)=>{
            if(res.data.code==='Email_has_been_registered')
            {
                message.warning('邮箱已被注册');
            }
            else if(res.data.code==='SUCCESS')
            {
                message.success('注册成功,请登录');
                window.location.href="/";
                dispatch({
                    type:CHANGE_PAGE_SUCCSEE,
                    data:1
                })
                dispatch({
                    type:GET_REGISTER_SUCCESS,
                    data:res.data
                })
            }
            else if(res.data.code==='Email_code_wrong')
            {
                message.warning("注册失败");
            }
           
        }).catch((err)=>{
            dispatch({
                type:GET_USERS_LOGIN_FAILURE,
                data:false
            })
            message.warning("注册出错");
        })
    }

}

export function sendLogin(query='')
{
   Storage.setStorage('username',query.email);
    return async(dispatch) => {
        axios({
            method: 'POST',
            url: `${baseUrl}user.do/login`,
            data: JSON.stringify(query),
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        dataType:'json',
          
        }).then((data)=>{

            data=data.data;
            console.log(data.code);
            if(data.code==='SUCCESS'){
                Storage.setStorage("token",data.token);
                dispatch({
                    type: GET_USERS_LOGIN_SUCCESS,
                    data:true
                })
             window.location.href="/";
            }
            else if(data.msg==='verify_code_wrong')
            {
                
                Storage.removeStorage('username');
                message('验证码错误');
            }
            else if(data.msg==='possword_wrong')
            {
                
                Storage.removeStorage('username');
                message.warning('账号或密码错误');
            }
            else
            {
                Storage.removeStorage('username');
                message.warning('登录失败');
            }
        }).catch((err)=>{
            dispatch({
                type:GET_USERS_LOGIN_FAILURE,
                data:false
            })
            Storage.removeStorage('username');
            alert('登录出错了');
        })
    };

}
export function getVerification()
{
   // console.log
    return async(dispatch) => {
        try {
           const data = (await axios.post(`${baseUrl}user.do/getVerCode`)).data;
         //  console.log("dataV:",data);
            dispatch({
                type:GET_VERIFICATION_SUCCESS,
                data:data
            })
        
        } catch (error) {
            message.info("验证码获取失败");
        }
    };

}
/**
 * 改变登录注册页面，1为登录，2为注册
 * @param {*} num 
 */
export const changePage=(num)=>
{
    return (dispatch)=>{
        dispatch({
            type:CHANGE_PAGE_SUCCSEE,
            data:num

        })
    }
}

export function sendEmail(mail)
{
  console.log(mail);
    return async(dispatch) => {
        try {
           const data = (await axios.post(`${baseUrl}user.do/getMailCode?emailId=`+mail)).data;
           console.log("dataM:",data);
           if(data.code==='SUCCESS')
           {
            dispatch({
                type:GET_ESENDEMAIL_SUCCESS,
                data:data
            })
           }
           else if(data.code==='data.get_email_code_failed')
           {
                message.warning("邮箱格式错误或者邮箱不存在");
           }
           else if(data.code==='can_not_get_verifycode')
           {
                message.warning("可能是redis服务器崩了");
           }
           else if(data.code==='email_can_not_be_null')
           {
                message.warning('邮箱不能为空');
           }
           else
           {
                message.warning('系统错误');
           }
        } catch (error) {
            message.info("验证码获取失败");
        }
    };

}




